package org.example;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.util.Objects;

public class Client extends JFrame implements ActionListener {
    JLabel Lser=null, Lid=null, Lpsw=null;
    JTextField Tser=null, Tid=null, Tpsw=null, Tsee=null;
    JButton Bget=null, Bsend=null, Bsee=null, Bs=null, Bdele=null, Bchk=null;
    JTable mails=null;
    DefaultTableModel model = null;
    JScrollPane jsp, jsp2, jsp3 = null;
    JPanel jp1, jp2,jp3,jp4,jp5,jp6 = null;
    JTextArea area1, area2= null;

    String line;
    static Socket socket;
    static InputStream input;
    static OutputStream output;
    static BufferedReader reader;
    static PrintWriter writer;
    static String ser_ip;
    static String sender;
    static String password;

    public Socket getSocket(){
        return socket;
    }
    public InputStream getInputStream(){
        return input;
    }
    public OutputStream getOutputStream(){
        return output;
    }
    public BufferedReader getBufferedReader(){
        return reader;
    }
    public PrintWriter getPrintWriter(){
        return writer;
    }

    public static void main(String[] args) {
        Client win=new Client();
    }

    public  Client() {
        Lser= new JLabel("服务器");
        Lid= new JLabel("用户名");
        Lpsw= new JLabel("密码");

        Tser= new JTextField(12);
        Tser.setText("smtp.qq.com");
        Tid= new JTextField(20);
        Tid.setText("1246625854@qq.com");
        Tpsw= new JTextField(20);
        Tpsw.setText("vlxcqhljgcaggahi");
        Tsee= new JTextField(3);

        Bget= new JButton("接收邮件");
        Bsend= new JButton("发送邮件");
        Bsee= new JButton("查看邮件");
        Bs= new JButton("链接");
        Bdele= new JButton("删除邮件");
        Bchk= new JButton("查询列表");
        Bsee.addActionListener(this);
        Bget.addActionListener(this);
        Bsend.addActionListener(this);
        Bs.addActionListener(this);
        Bdele.addActionListener(this);
        Bchk.addActionListener(this);

        // 设置表格
        String[] colnames = {"序号", "大小",};
        model = new DefaultTableModel(colnames, 31);
        mails = new JTable(model);
        mails.setPreferredScrollableViewportSize(new Dimension(400,120));
        jsp = new JScrollPane(mails);

        area1= new JTextArea(20,70);
        area2= new JTextArea(7, 70);
        area1.setVisible(true);
        area2.setVisible(true);
        jsp2 = new JScrollPane();
        jsp2.setViewportView(area1);
        jsp3 = new JScrollPane();
        jsp3.setViewportView(area2);

        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();
        jp4 = new JPanel();
        jp5 = new JPanel();
        jp6 = new JPanel();

//        jp1.setLayout(new GridLayout(4,1));
        jp1.add(Lser); jp1.add(Tser); jp1.add(Lid); jp1.add(Tid); jp1.add(Lpsw); jp1.add(Tpsw); jp1.add(Bs);
//        jp2.setLayout(new GridLayout(2,1));
        jp2.add(Bget); jp2.add(Bsend);
        jp3.add(jsp3);
        jp4.add(jsp);
        jp5.add(Tsee); jp5.add(Bsee); jp5.add(Bchk); jp5.add(Bdele);
        jp6.add(jsp2);

        jp1.setBounds(50,10, 800,40);
        jp3.setBounds(50,60, 800,120);
        jp2.setBounds(50,190, 800,40);
        jp4.setBounds(50,235, 800,125);
        jp5.setBounds(50,365, 800,40);
        jp6.setBounds(50,410, 800,600);

        this.add(jp1); this.add(jp3); this.add(jp2); this.add(jp4); this.add(jp5); this.add(jp6);

//        this.setLayout(new GridLayout(6,1));
        this.setLayout(null);
        this.setTitle("邮箱极简版");
        this.setSize(900, 800);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("链接") ) {
            ser_ip = Tser.getText();
            sender = Tid.getText();
            password = Tpsw.getText();
            String ID = Base64.ToBase64(sender.getBytes());
            String pass = Base64.ToBase64(password.getBytes());
            try {
                socket = new Socket(ser_ip, 25);
                input = socket.getInputStream();
                output = socket.getOutputStream();
                reader = new BufferedReader(new InputStreamReader(input));
                writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "gbk"), true);
                area2.setText(">telnet "+ser_ip+" 25"+ "\n");
                area2.append(reader.readLine()+ "\n");

                writer.println("HELO gxy");
                area2.append(">HELO gxy"+ "\n");
                area2.append(reader.readLine()+ "\n");
                area2.append(reader.readLine()+ "\n");
                area2.append(reader.readLine()+ "\n");

                //AUTH LOGIN
                writer.println("auth login");
                area2.append(">auth login"+ "\n");
                area2.append(reader.readLine()+ "\n");
                writer.println(ID);
                area2.append(reader.readLine()+ "\n");
                writer.println(pass);
                line=reader.readLine();
                area2.append(line+ "\n");
                if(line.equals("235 Authentication successful")) {
                    area2.append("连接成功");
                }else {
                    area2.append("连接失败");
                }

                input.close();
                output.close();
                socket.close();
            }catch (Exception eo) {
                eo.printStackTrace();
            }

        }
        else if (e.getActionCommand().equals("发送邮件")) {
            Sender send= new Sender(ser_ip, sender, password);
        }
        else if (e.getActionCommand().equals("接收邮件")) {
            try {
                socket = new Socket(ser_ip, 110);
                input = socket.getInputStream();
                output = socket.getOutputStream();
                reader = new BufferedReader(new InputStreamReader(input, "gbk"));
                writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "gbk"), true);
                area2.setText(">telnet "+ser_ip+" 110"+ "\n");
                area2.append(reader.readLine()+ "\n");
                writer.println("USER "+sender);
                area2.append(">USER "+ sender+ "\n");
                area2.append(reader.readLine()+ "\n");

                writer.println("PASS "+ password);
                area2.append(">PASS "+ password+ "\n");
                area2.append(reader.readLine()+ "\n");

                writer.println("STAT");
                area2.append(">STAT"+ "\n");
                line=reader.readLine();
                area2.append(line+ "\n");

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        else if  (e.getActionCommand().equals("查看邮件")) {
            String i= Tsee.getText();
            try {
                writer.println("RETR "+ i);
                area2.append(">RETR "+ i+ "\n");
                area2.append(reader.readLine()+ "\n");
                line=reader.readLine();
                area1.setText(line+ "\n");
                while (!Objects.equals(line, ".")) {
                    line=reader.readLine();
                    area1.append(line+ "\n");
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        else if  (e.getActionCommand().equals("查询列表")) {
            try {
                writer.println("LIST");
                area2.append(">LIST"+ "\n");
                area2.append(reader.readLine()+ "\n");
                line=reader.readLine();
                int x=0, j;
                while (!Objects.equals(line, "." ) && x<30) {
                    String []g= line.split(" ");
                    j=0;
                    for(String m : g) {
                        mails.setValueAt(m, x, j++);
                    }
                    line=reader.readLine();
                    x++;
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        else if  (e.getActionCommand().equals("删除邮件")) {
            String i= Tsee.getText();
            try {
                writer.println("DELE "+ i);
                area2.append(">DELE "+ i+ "\n");
                area2.append(reader.readLine()+ "\n");
                writer.println("QUIT");
                area2.append(">QUIT"+ "\n");
                area2.append(reader.readLine()+ "\n");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
